package com.school.base.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.school.base.dao.ISubjectDao;
import com.school.base.model.Subject;
import com.school.common.base.BaseDao;

public class SubjectDao extends BaseDao implements ISubjectDao {

	@Override
	public Subject getSubjectById(int id) {
		return (Subject) this.getObjectById(Subject.class, id);
	}

	@Override
	public List<Subject> getSubjectList(Subject subject, int offset, int pageSize) {
		return this.getList(Subject.class, null, null, offset, pageSize);
	}

	@Override
	public int getTotalCount(Subject subject) {
		return this.getRecordCount(Subject.class);
	}

	@Override
	public void updateSubject(Subject subject) {
		this.update(subject);
	}

	private boolean isUsed(int subjectId) {
		String sql = "select count(*) from b_teacher where subject_id=" + subjectId;
		int count = this.getRecordCountBySql(sql);
		return count > 0;
	}

	@Override
	public void deleteSubject(int id) throws Exception {
		if (this.isUsed(id)) {
			logger.error("Delete Subject faile,is Used.");
			throw new Exception("科目已经被使用，不能删除");
		}
		Subject subject = new Subject();
		subject.setId(id);
		this.delete(subject);
	}

	@Override
	public void insertSubject(Subject subject) {
		this.insert(subject);
	}

	@Override
	public List<Subject> getAll() {
		return this.getList(Subject.class, null, null);
	}

	private int getLastPosition() {
		String hql = "select position from Subject order by position desc limit 1";
		return this.getRecordCountByHql(hql);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.school.base.dao.ISubjectDao#moveUp(int)
	 */
	@Override
	public void moveUp(int id) {
		Subject subject = this.getSubjectById(id);
		if (subject != null) {
			List<Criterion> express = new ArrayList<Criterion>();
			express.add(Restrictions.lt("position", subject.getPosition()));
			List<Order> order = new ArrayList<Order>();
			order.add(Order.desc("position"));
			List list = this.getList(Subject.class, express, order, 0, 1);
			if (list != null && list.size() > 0) {
				Subject s = (Subject) list.get(0);
				int position = s.getPosition();
				s.setPosition(subject.getPosition());
				subject.setPosition(position);
				this.update(s);
				this.update(subject);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.school.base.dao.ISubjectDao#moveDown(int)
	 */
	@Override
	public void moveDown(int id) {
		Subject subject = this.getSubjectById(id);
		if (subject != null && subject.getPosition() < this.getLastPosition()) {
			List<Criterion> express = new ArrayList<Criterion>();
			express.add(Restrictions.gt("position", subject.getPosition()));
			List<Order> order = new ArrayList<Order>();
			order.add(Order.asc("position"));
			List list = this.getList(Subject.class, express, order, 0, 1);
			if (list != null && list.size() > 0) {
				Subject s = (Subject) list.get(0);
				int position = s.getPosition();
				s.setPosition(subject.getPosition());
				subject.setPosition(position);
				this.update(s);
				this.update(subject);
			}
		}
	}

}
